package farmers.yavtube;

import farmers.yavtube.util.FileUtil;
import farmers.yavtube.util.HttpUtil;
import farmers.yavtube.util.MyThread;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;

public class YavtubeN0553 {

    public static ExecutorService pool = Executors.newFixedThreadPool(20);  // 创建一个可重用线程数的线程池
    public static long startTime = System.currentTimeMillis();
    static {
        HttpUtil.setProxyServer("127.0.0.1", "10792");
    }

	// 照片下载
	public static void main(String[] args) throws Exception {
        // 分析
        String analySrc = "n0553芹川レイラ https://img.javcup.com/japanese/reira-serikawa/1/reira-serikawa-1.jpg 38";
		String[] analyArr = analySrc.split(" ");//分析字符串
		String analyName = analyArr[0];//名字
		String analyUrl = analyArr[1];//第一组图片第一张的地址
		int analyCount = Integer.valueOf(analyArr[2]);//有多少组图片

		String downFolder = "f:\\vphoto2\\" + analyName + "\\";//下载路劲

		for (int j = 1; j <= analyCount ; j++) {
			for (int i = 1; i <= 12; i++) {//单组图片的数量
				String imgSrc = String.format(analyUrl.replace("1", "%d"), j, i);//格式化 下载地址
				String fileName = String.format(analyName + "-%02d-%02d.jpg", j, i);//格式化 文件名
				pool.execute(new MyThread(imgSrc, downFolder, fileName));
				TimeUnit.SECONDS.sleep(1);
			}
		}

		// 关闭线程池
		pool.shutdown();

        long endTime = System.currentTimeMillis();
        System.out.println("任务结束 耗时：" + ((endTime - startTime)/1000) + "秒");
	}

    /**
     * 单组照片 下载
     */
    public static void main2(String[] args) {
        List<String> list = FileUtil.readImgUrls();

        int analyIndex = 1;// 开始序号
        String analyName = "杏奈りか";

        for (String temp : list) {
            String downFolder = "F:\\vphoto2\\" + analyName + "\\";//下载路劲
            String analyUrl = temp;//图片地址
            int analyCount = 99;// 一组多少张照片

            if(temp.length() == 0 || temp.startsWith("#")){
                continue;
            }

            if(temp.split(" ").length == 1){
                analyUrl = temp.split(" ")[0];
                analyCount = 99;
            }else if(temp.split(" ").length > 1){
                analyUrl = temp.split(" ")[0];
                analyCount = Integer.valueOf(temp.split(" ")[1]);
            }

            for (int j = 1; j <= analyCount ; j++) {
                String imgSrc = String.format(analyUrl.replace("1.jpg", "%d.jpg"), j);//格式化 下载地址
//                System.out.println(imgSrc);
                String fileName = String.format(analyName + "-%03d-%02d.jpg", analyIndex, j);//格式化 文件名
//                System.out.println(fileName);
                pool.execute(new MyThread(imgSrc, downFolder, fileName));
            }
            analyIndex++;//序号递增
        }

        // 关闭线程池
        pool.shutdown();
    }

}

